package cn.edu.tju.se.auth.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.tju.se.auth.repository.UdrRepository;
import cn.edu.tju.se.auth.enity.Group;
import cn.edu.tju.se.auth.enity.Role;
import cn.edu.tju.se.auth.enity.Udr;
import cn.edu.tju.se.auth.enity.User;
import cn.edu.tju.se.auth.service.GroupService;
import cn.edu.tju.se.auth.service.RoleService;
import cn.edu.tju.se.auth.service.UdrService;
import cn.edu.tju.se.auth.service.UserService;

@Service
public class UdrServiceImpl implements UdrService {

	@Autowired UdrRepository udrRepository;
	@Autowired UserService userService;
	@Autowired GroupService groupService;
	@Autowired RoleService tjqsRoleService;
	@Override
	public Set<Udr> getByRoleId(Long roleId) {
		return tjqsRoleService.get(roleId).getMyUdr();
	}
	@Override
	public Set<Udr> getByUserIdAndGroupId(Long userId, Long groupId) {
		return udrRepository.findByUserIdAndGroupId(userId, groupId);
	}
	@Override
	public Set<Udr> getByUserId(Long userId) {
		return userService.findOne(userId).getMyUdr();
	}
	@Override
	public List<Role> getMyRoles(Long userId) {
		List<Role> roles=new ArrayList<Role>();
		Set<Udr> source=getByUserId(userId);
		for(Udr udr:source) {
			roles.add(udr.getRole());
		}
		return roles;
	}
	@Override
	public List<Group> getMyGroups(Long userId) {
		List<Group> groups=new ArrayList<Group>();
		Set<Udr> source=getByUserId(userId);
		for(Udr udr:source) {
			groups.add(udr.getGroup());
		}
		return groups;
	}
	@Override
	public List<String> getMyRolesNames(Long userId) {
		List<String> result=new ArrayList<String>();
		List<Role> roles=getMyRoles(userId);
		for(Role role:roles) {
			result.add(role.getName());
		}
		return result;
	}
	@Override
	public Udr save(Udr udr) {
		return udrRepository.save(udr);
		
	}
	@Override
	public List<Udr> saveAll(List<Udr> udr) {
		return udrRepository.save(udr);
	}
	@Override
	public void delete(int id) {
		Udr ud= udrRepository.findOne(id);
		ud.getUser().getMyUdr().remove(ud);
		ud.getRole().getMyUdr().remove(ud);
		ud.getGroup().getMyUdr().remove(ud);
		udrRepository.delete(id);
	}
	@Override
	public void deleteAll(Set<Udr> udr,String type) {
		removeFromFathers(udr,type);
		udrRepository.delete(udr);
	}
	@Override
	public void update(Udr udr) {
		udrRepository.save(udr);
		
	}
	private void removeFromFathers(Set<Udr> udr,String type) {
		for(Udr ud:udr) {
			User u=ud.getUser();
			Role r=ud.getRole();
			Group g=ud.getGroup();
			switch(type) {
				case "group":
					u.getMyUdr().remove(ud);
					r.getMyUdr().remove(ud);
					break;
				case "user":
					g.getMyUdr().remove(ud);
					r.getMyUdr().remove(ud);
					break;
				case "role":
					u.getMyUdr().remove(ud);
					g.getMyUdr().remove(ud);
					break;
				case "all":
					u.getMyUdr().remove(ud);
					g.getMyUdr().remove(ud);
					r.getMyUdr().remove(ud);
			}
			
		}
	}
	
}
